﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;

namespace Bot.Application.Subscriptions.Service
{
    public class SubscriptionAppService : BaseAppService, ISubscriptionAppService
    {
        private ISubscriptionDomainService _domainService;
        private IDetectIntentRecordDomainService _usageDomainService;

        public SubscriptionAppService(ISubscriptionDomainService domainService, IDetectIntentRecordDomainService usageDomainService) : base()
        {
            this._domainService = domainService;
            this._usageDomainService = usageDomainService;
        }

       
        public SubscriptionDto Get(string include)
        {
            Subscription subscription = _domainService.Get();
            bool isChatbotEnabled = false; //todo: 需要判断bot addon, (Lisence or NextbillingDate)
            if (subscription == null)
            {
                subscription = new Subscription()
                {
                    StartDate = DateTime.MinValue,
                    NextRenewDate = DateTime.MinValue,
                };
            }
            isChatbotEnabled = subscription.NextRenewDate >= DateTime.UtcNow;

            var dto = Mapper.Map<SubscriptionDto>(subscription);
            dto.IsChatbotEnabled = isChatbotEnabled;
            if (isChatbotEnabled)
            {
                dto.ChatbotUsages = Mapper.Map<List<ChatbotUsageDto>>(
                _usageDomainService.GetChatbotUsages(subscription.NextRenewDate.AddMonths(-1), subscription.NextRenewDate, include));
            }
            return dto;
        }
    }
}
